// 数据格式转换，把pid树型数据结构的转成适合组织树的的数据格式。
export default function (d, pid, id) {
    var pidList = d;
    var afterList = d;
    pid = pid || 'pid';
    id = id || 'id';
    cnode(pidList);
    return afterList;
    function cnode (d) {
        var n = afterList.length;
        var newData = null;
        var l = d.length;
        var i = 0;
        for (i = 0;i < l;i++) {
            if (n > 0) {
                newData = set(d[i][id], afterList);
                afterList = newData.data;
                if (newData.nodelist.length > 0) {
                    d[i].children = newData.nodelist;
                    cnode(d[i].children);
                }
            }
        }
    }

    function set (n, data) {
        n = n + '';
        var t = 0, l = 0, arr1 = [], arr2 = [], i = 0;
        l = data.length;
        for (i = 0;i < l;i++) {
            t = data[i][pid] + '';
            if (t === n) {
                arr1.push(data[i]);
            } else {
                arr2.push(data[i]);
            }
        }
        return { nodelist: arr1, data: arr2 };
    }
};
